import AparkApi

# 以下密钥信息从控制台获取
appid = "ca82a378"  # 填写控制台中获取的 APPID 信息
api_secret = "ZTY3MDU2MThjMzAyOGMzZWE4ODI4OTA0"  # 填写控制台中获取的 APISecret 信息
api_key = "ec681f08e0087413b7d836947e910517"  # 填写控制台中获取的 APIKey 信息

domain = 'generalv3.5' # v3版本
# 云端环境的服务地址
Spark_url = "wss://spark-api.xf-yun.com/v3.5/chat"  # v3环境的地址（"wss://spark-api.xf-yun.com/v3.1/chat）

text = []


# length = 0

def getText(role, content):
    jsoncon = {}
    jsoncon["role"] = role
    jsoncon["content"] = content
    text.append(jsoncon)
    return text


def getlength(text):
    length = 0
    for content in text:
        temp = content["content"]
        leng = len(temp)
        length += leng
    return length


def checklen(text):
    while (getlength(text) > 8000):
        del text[0]
    return text


if __name__ == '__main__':
    text.clear()
    while 1:
        Input = input("\n" + "我:")
        question = checklen(getText("user", Input))
        AparkApi.answer = ""
        print("星火:", end="")
        AparkApi.main(appid, api_key, api_secret, Spark_url, domain, question)
        getText("assistant", AparkApi.answer)
        # print(str(text))
